﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;

namespace ASCalpuff.FileLayers
{
    /// <summary>
    /// 文件管理器
    /// </summary>
    public class FileSystem
    {

        protected FileSystem()
        {

        }

        static FileSystem m_fileSystem;
        public static FileSystem GetFileSystem()
        {
            if (m_fileSystem == null)
            {
                m_fileSystem = new FileSystem();
                return m_fileSystem;
            }

            else
            {
                return m_fileSystem;
            }
        }


        /// <summary>
        /// 将文本文件写入到硬盘
        /// </summary>
        /// <param name="pfname"></param>
        /// <returns></returns>
        public bool WriteFile(string pfname, string content, bool replaceFile)
        {
            FileInfo MyFile = new FileInfo(pfname);
            if (MyFile.Exists == true)
            {
                if (replaceFile)
                {
                    try
                    {
                        MyFile.Attributes = FileAttributes.Normal;
                        MyFile.Delete();
                        FileStream FS = MyFile.Create();
                        FS.Close();
                        MyFile.Attributes = FileAttributes.Normal;
                        StreamWriter Sw = MyFile.AppendText();

                        Sw.Write(content, Encoding.GetEncoding("GB2312"), false);
                        Sw.Flush();
                        Sw.Close();
                        return true;
                    }
                    catch (Exception ex)
                    {
                       
                        return false;
                    }

                }
                else
                    return false;


            }
            else
            {
                try
                {
                    FileStream FS = MyFile.Create();

                    FS.Close();
                    MyFile.Attributes = FileAttributes.Normal;
                    StreamWriter Sw = MyFile.AppendText();
                    Sw.Write(content, Encoding.GetEncoding("GB2312"), false);
                    Sw.Flush();
                    Sw.Close();
                }
                catch (Exception ex)
                {

                   
                    return false;
                }


            }

            return true;
        }



        public bool WriteFile(string path, string fname, string argName, string argValue)
        {
            return true;

        }


        /// <summary>
        /// 读取对应的文本文件，到字符串中
        /// </summary>
        /// <param name="pfname">包含路径的完整文件名</param>
        /// <returns></returns>
        public string[] ReadFileAsString(string pfname)
        {
            //string[] result = System.IO.File.ReadAllText(pfname).Split(',');
            // //数量 
            //// result.Length;

            // //按照你的要求
            // string content = System.IO.File.ReadAllText(@"C:\C#.txt");
            // string[] contentArray = content.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

            // //放的列表
            // List<int[]> list = new List<int[]>();

            // for (int i = 0; i < contentArray.Length; i += 3)
            // {
            //     int a, b, c;
            //     a = 0;
            //     b = 0; 
            //     c = 0;
            //     //判断一下，防止上标出界
            //     if (i + 2 <= contentArray.Length)
            //     {
            //         //防止数据异常
            //         if (!int.TryParse(contentArray[i], out a) || !int.TryParse(contentArray[i + 1], out b) || !int.TryParse(contentArray[i + 2], out c))
            //         {
            //             int[] res = new int[3];
            //             res[0] = a;
            //             res[1] = b;
            //             res[2] = c;

            //             list.Add(res);
            //         }
            //     }
            // }
            string[] str;

            str = File.ReadAllLines(pfname);

            return str;


        }


        /// <summary>
        /// 读取对应的文本文件，到字符串中
        /// </summary>
        /// <returns>带有组名称的字符串</returns>
        public Dictionary<string, string[]> ReadFileWithGroup(string pfname)
        {
            string[] str;
            string name = "";
            List<string> li = new List<string>();
            int b = 0;
            Dictionary<string, string[]> m_dic = new Dictionary<string, string[]>();

            FileStream file = new FileStream(pfname, FileMode.Open, FileAccess.Read);
            StreamReader sr = new StreamReader(file);
            string s = sr.ReadLine();
            while (s != null)
            {
                s = sr.ReadLine();
                if (s != null && s.Length > 0)
                {
                    if (b == 1)
                    {
                        if (this.GetString(s, 1) == "INPUT" & this.GetString(s, 2) == "GROUP:")
                        {
                            if (li.Count > 0)
                            {
                                str = li.ToArray();
                                m_dic.Add(name, str);
                                li.Clear();
                            }
                        }
                        if (this.GetString(s, 1) == "INPUT" & this.GetString(s, 2) == "GROUPS:")
                        {
                            if (li.Count > 0)
                            {
                                str = li.ToArray();
                                m_dic.Add(name, str);
                                li.Clear();
                            }
                        }
                    }
                    if (this.GetString(s, 1) == "INPUT" & this.GetString(s, 2) == "GROUP:")
                    {
                        b = 1;
                        name = this.GetString(s, 2) + this.GetString(s, 3);
                    }
                    if (this.GetString(s, 1) == "INPUT" & this.GetString(s, 2) == "GROUPS:")
                    {
                        b = 1;
                        name = this.GetString(s, 2) + this.GetString(s, 3);
                    }
                    if (b == 1)
                    {
                        li.Add(s);
                    }
                }
            }
            sr.Close();
            if (li.Count > 0)
            {
                str = li.ToArray();
                m_dic.Add(name, str);
                li.Clear();
            }
            return m_dic;
        }

        public bool BeginReadFile(string pfname)
        {
            return true;

        }

        public bool EndReadFile(string pfname)
        {
            return true;

        }


        public String GetString(String str, int index)
        {
            str = "  " + str + "  ";//前后各增加两个空格，以便于正确处理
            String substring = "";
            int n = 0;
            int n1 = 0;
            int n2 = 0;
            try
            {

                for (int i = 0; i < str.Length - 1; i++)
                {
                    if (str.Substring(i, 1) != " ")
                    {
                        n1 = i;
                        n2 = str.IndexOf(" ", n1);
                        substring = str.Substring(n1, n2 - n1);
                        i = n2;
                        n = n + 1;
                        if (n == index)
                        {
                            return substring;

                        }
                    }
                }

            }
            catch
            {
                return null;
            }
            return null;
        }



        /// <summary>
        /// 复制文件
        /// </summary>
        /// <param name="pfName">源文件路径</param>
        /// <param name="dstName">目标文件路径</param>
        /// <returns></returns>
        public bool CopyFile(string pfName, string dstName)
        {
            FileInfo F1 = new FileInfo(pfName);

            if (F1.Exists)
            {
                try
                {
                    FileInfo F2 = F1.CopyTo(dstName, true);  //允许覆盖原文件

                    if (F2 == null)
                    {
                        return false;
                    }
                    else
                    {
                        return true;
                    }

                }
                catch (Exception ex)
                {
                   
                    return false;
                }
            }
            else
            {
               
                return false;
            }


        }

        /// <summary>
        /// 删除指定文件
        /// </summary>
        /// <param name="pfName">要删除文件的路径</param>
        /// <returns></returns>
        public bool DeleteFile(string pfName)
        {
            FileInfo F = new FileInfo(pfName);

            if (F.Exists)
            {
                try
                {
                    F.Attributes = FileAttributes.Normal; //设置属性
                    F.Delete();
                    return true;
                }
                catch (Exception ex)
                {
                    
                    return false;
                }

            }
            else
            {
                
                return false;
            }


        }

        /// <summary>
        /// 向指定文件写入一行字符
        /// </summary>
        /// <param name="pfName">文件路径</param>
        /// <param name="LineString">写入的字符串</param>
        /// <returns></returns>
        public bool WriteLineString(string pfName, string LineString)
        {

            FileInfo F = new FileInfo(pfName);
            if (F.Exists)
            {
                try
                {
                    F.Attributes = FileAttributes.Normal;//设置属性，可写入
                    StreamWriter SW = F.AppendText();

                    SW.WriteLine(LineString);
                    SW.Flush();
                    SW.Close();

                    return true;

                }
                catch (Exception ex)
                {
                   
                    return false;

                }
            }
            else
            {
                
                return false;
            }



        }
      



    }


}
